<template>
  <div>
    <div class="search-bar" @click="$router.push('/search')">
      <div class="search">
        <van-icon name="search" size="18" />
        <p class="words">搜索鲜花、蛋糕、xxx</p>
      </div>
    </div>
    <div class="cate-container">
      <div class="cate-wrap">
        <div class="cates">
          <div
            v-for="(cate, index) in cates"
            :key="cate.id"
            @click="switchCate(index)"
            :class="['cate', {active: activeIndex===index}]">{{cate.name}}</div>
        </div>
      </div>
      <div class="item-wrap">
        <div class="items-wrap">
          <div
            class="item"
            @click="$router.push(`/detail/${item.id}`)"
            v-for="item in items"
            :key="item.id"
          >
            <img class="item-pic" :src="item.pic" alt="" />
            <div class="item-info">
              <h5 class="name">{{ item.name }}</h5>
              <p class="desc">
                {{ item.characteristic ? item.characteristic : "暂无描述" }}
              </p>
              <div class="action">
                <p class="price">￥{{ item.minPrice }}</p>
                <van-icon
                  size="24"
                  color="#ff734c"
                  name="jiarugouwuche"
                  class-prefix="icon"
                ></van-icon>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <common-tab />
  </div>
</template>

<script>
import CommonTab from '../../components/CommonTab.vue'
import { fetchCates, fetchItems } from '@api'
export default {
  data () {
    return {
      cates: [], // 所有分类
      items: [], // 分类对应列表商品
      activeIndex: 0
    }
  },
  created () {
    this.fetchCates()
  },
  methods: {
    switchCate (index) {
      this.activeIndex = index
      this.fetchItems()
    },
    fetchCates () { // 请求所有分类
      fetchCates().then(res => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          this.fetchItems()
        }
      })
    },
    fetchItems () {
      fetchItems({
        categoryId: this.cates[this.activeIndex].id
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.result
        }
        if (res.data.code === 700) {
          this.items = []
        }
      })
    }
  },
  components: { CommonTab },
  name: 'Cate'
}
</script>

<style lang="scss" scoped>
.search-bar {
  width: 10rem;
  height: 50px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  .search {
    width: 90%;
    height: 40px;
    background-color: #f3f5f7;
    border-radius: 20px;
    padding: 0 10px;
    display: flex;
    align-items: center;
    color: #666;
    font-size: 14px;
    .words {
      margin-left: 10px;
    }
  }
}
.cate-container {
  position: fixed;
  left: 0;
  right: 0;
  top: 50px;
  bottom: 50px;
  display: flex;
  background-color: #fff;
  .cate-wrap {
    width: 2.5rem;
    height: 100%;
    overflow-y: auto;
    border-right: 1px solid #999;
    .cate {
      color: #333;
      font-size: 16px;
      text-align: center;
      line-height: 30px;
      margin: 10px 0;
      position: relative;
      &.active {
        color: #ff734c;
        &::after {
          content: "";
          height: 30px;
          width: 2px;
          background-color: #ff734c;
          position: absolute;
          left: 0;
          top: 0;
        }
      }
    }
  }
  .item-wrap {
    width: 7.5rem;
    height: 100%;
    overflow-y: auto;
    .items-wrap {
      padding: 0 5px;
      .item {
        display: flex;
        border-radius: 8px;
        background-color: #fff;
        overflow: hidden;
        margin-top: 5px;
        img.item-pic {
          width: 3rem;
        }
        .item-info {
          flex: 1;
          padding-left: 8px;
          padding-right: 15px;
          .name {
            color: #333;
            font-size: 16px;
            line-height: 40px;
          }
          .desc {
            color: #666;
            font-size: 14px;
            line-height: 20px;
            width: 3rem;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
          }
          .action {
            display: flex;
            align-items: center;
            justify-content: space-between;
            .price {
              color: #333;
              font-size: 16px;
            }
          }
        }
      }
    }
  }
}
</style>
